﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//枚举映射到Nhibernate,否则取出的数据会每次自动update
namespace OA.Common.EnumTypeMapping
{
    /// <summary>
    /// nhibernate类型映射抽象类
    /// </summary>
    public abstract class NhibernateUserType : NHibernate.UserTypes.IUserType
    {
        private Type _type;
        private int _length;
        private System.Data.DbType _dbtype;

        public NhibernateUserType(Type type, System.Data.DbType dbtype, int length)
        {
            this._type = type;
            this._length = length;
            this._dbtype = dbtype;
        }

        /// <summary>
        /// 从缓存中取对象
        /// </summary>
        public object Assemble(object cached, object owner)
        {
            return DeepCopy(cached);
        }

        /// <summary>
        /// 如果IsMutable为true，此处应当实现对象实例的DeepCopy
        /// </summary>
        public object DeepCopy(object value)
        {
            return value;
        }

        /// <summary>
        /// 将对象放入缓存前的处理
        /// </summary>
        public object Disassemble(object value)
        {
            return DeepCopy(value);
        }

        public new bool Equals(object x, object y)
        {
            return x.GetHashCode() == y.GetHashCode();
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }

        /// <summary>
        /// 自定义类型的对象实例是否会发生改变
        /// </summary>
        public bool IsMutable
        {
            get { return false; }
        }

        /// <summary>
        /// 读取数据库字段值（DataReader），转换成实体属性
        /// </summary>
        public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
        {
            object name = NHibernate.NHibernateUtil.String.NullSafeGet(rs, names[0]);
            if (name == null) return null;
            return Enum.Parse(_type, name.ToString(), true);
        }

        /// <summary>
        /// 为属性的存取设置DbCommand参数
        /// </summary>
        public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
        {
            NHibernate.NHibernateUtil.String.NullSafeSet(cmd, value, index);
        }

        public object Replace(object original, object target, object owner)
        {
            return original;
        }

        public Type ReturnedType
        {
            get { return _type; }
        }

        /// <summary>
        /// 属性对应的数据库字段类型
        /// </summary>
        public NHibernate.SqlTypes.SqlType[] SqlTypes
        {
            get { return new NHibernate.SqlTypes.SqlType[] { new NHibernate.SqlTypes.SqlType(_dbtype, this._length) }; }
        }
    }

    public class ResourceType : NhibernateUserType
    {
        public ResourceType()
            : base(typeof(EnumType.ResourceType), System.Data.DbType.Int32, 4)
        {
        }
    }
    public class RoleUserType : NhibernateUserType
    {
        public RoleUserType()
            : base(typeof(EnumType.RoleUserType), System.Data.DbType.Int32, 4)
        {
        }
    }
    public class DictionaryType : NhibernateUserType
    {
        public DictionaryType()
            : base(typeof(EnumType.DictionaryType), System.Data.DbType.Int32, 4)
        {
        }
    }

    public class ManagerType : NhibernateUserType
    {
        public ManagerType()
            : base(typeof(EnumType.ManagerType), System.Data.DbType.Int32, 4)
        {
        }
    }
}
